package Controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import Bean.BeanBook;
import Impl.BookServiceImpl;
import Impl.RatingServiceImpl;
import index.Experiment1;
import index.Experiment2;
import index.Item;
import index.Item_ori;
/**
 * Servlet implementation class IndexCreateController
 */
@Controller
public class IndexCreateController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	LinkedList<Item_ori> list = new LinkedList<>();
	BookServiceImpl bookService=new BookServiceImpl();
	@RequestMapping("/indexcreate")
	protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// TODO Auto-generated method stub
			ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
			bookService=(BookServiceImpl)applicationContext.getBean("BookServiceImpl");
			if(request.getSession().getAttribute("dic")==null) {
				ArrayList<BeanBook>books=bookService.getallbook();
				Experiment1 experiment1=new Experiment1();
				experiment1.partWord(books, list);
				list = experiment1.sortItems(list);
				experiment1.wordFrequency(list);
				LinkedList<Item> dictionary = experiment1.deduplAndCreateIndex(list);
				experiment1.outDictionary2File(dictionary, "result_exp1");
				request.getSession().setAttribute("dic", dictionary);
			}
			
			
	}
}
